package com.nineclock.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.nineclock.common.constant.SMSConstant;
import com.nineclock.common.entity.Result;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.redis.CacheService;
import com.nineclock.common.sms.SmsUtils;
import com.nineclock.common.utils.PhoneNumCheckUtils;
import com.nineclock.system.pojo.SysCompanyUser;
import com.nineclock.system.service.SmsService;
import com.nineclock.system.service.SysCompanyUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class SmsServiceImpl implements SmsService {

    @Autowired
    private CacheService cacheService;

    @Autowired
    private SmsUtils smsUtils;

    @Autowired
    private SysCompanyUserService sysCompanyUserService;

    /**
     * 发送短信验证码
     *
     * @param mobile
     * @param type
     * @return
     */
    @Override
    public Result sendCode(String mobile, String type) {

        if (StringUtils.isBlank(mobile) || !PhoneNumCheckUtils.isPhoneLegal(mobile)) {
            return Result.errorMessage(ResponseEnum.INVALID_PHONE_NUMBER.getMessage());
        }

        if (StringUtils.isBlank(type)) {
            return Result.errorMessage(ResponseEnum.INVALID_PARAM_ERROR.getMessage());
        }

        int count = sysCompanyUserService.count(Wrappers.<SysCompanyUser>lambdaQuery().eq(SysCompanyUser::getMobile, mobile));
        if (count != 0 && SMSConstant.SMS_TYPE_REGISTER.equals(type)) {
            return Result.errorMessage(ResponseEnum.USER_MOBILE_EMAIL_EXISTS.getMessage());
        }

        String code = smsUtils.generateValidateCode4String(6);

        boolean flag = smsUtils.sendSms(mobile, code);

        if (!flag) {
            return Result.errorMessage(ResponseEnum.SEND_MESSAGE_ERROR.getMessage());
        }

        String key = checkTypeToKey(type);
        if (key == null) {
            return Result.errorMessage(ResponseEnum.INVALID_PARAM_ERROR.getMessage());
        }

        key = key.concat(mobile);

        cacheService.set(key, code);
        cacheService.expire(key, 5, TimeUnit.MINUTES);
        return Result.success();
    }


    /**
     * 验证码登录验证
     *
     * @param mobile
     * @param type
     * @param checkcode
     * @return
     */
    @Override
    public Result verify(String mobile, String type, String checkcode) {

        if (StringUtils.isBlank(checkcode) || StringUtils.isBlank(mobile) || StringUtils.isBlank(type)) {
            return Result.errorMessage(ResponseEnum.INVALID_PARAM_ERROR.getMessage());
        }

        String key = checkTypeToKey(type);
        if (key == null) {
            return Result.errorMessage(ResponseEnum.INVALID_PARAM_ERROR.getMessage());
        }

        key = key.concat(mobile);

        String code = cacheService.get(key);

        if (StringUtils.isBlank(code)) {
            return Result.errorMessage(ResponseEnum.CODE_IMAGE_ERROR.getMessage());
        }

        if (!code.equals(checkcode)) {
            return Result.errorMessage(ResponseEnum.CODE_IMAGE_ERROR.getMessage());
        }

        cacheService.delete(key);

        return Result.success(ResponseEnum.SUCCESS);

    }


    /**
     * 根据类型返回对应验证码redis键值
     *
     * @param type
     * @return
     */
    String checkTypeToKey(String type) {

        String key = null;

        if (SMSConstant.SMS_TYPE_LOGIN.equals(type)) {
            key = SMSConstant.SMS_LOGIN_KEY_PREFIX;
        }

        if (SMSConstant.SMS_TYPE_REGISTER.equals(type)) {
            key = SMSConstant.SMS_REGISTER_KEY_PREFIX;
        }

        if (SMSConstant.SMS_TYPE_CHANGE_MANAGER.equals(type)) {
            key = SMSConstant.SMS_CHANGE_MANAGER_KEY_PREFIX;
        }

        if (SMSConstant.SMS_TYPE_RESET_PASSWORD.equals(type)) {
            key = SMSConstant.SMS_RESET_KEY_PREFIX;
        }

        return key;
    }

}
